package com.ChessApps.Network;

/**
 * Fichier : ServerChat.java Auteur : A. Toudeft Date : 15/05/2002 Sujet :
 * Exemple de communication avec des sockets Role : Programme serveur.
 */
import java.io.*;
import java.net.*;

public class ServerChat {

    int port;
    ServerSocket serveurSock;
    Socket[] tabSock;
    
    PrintWriter[] os;
    BufferedInputStream[] is;
    int max = 4;
    int nbConnection = 0;
    int playerReady;
    
    
    public ServerChat(int p) {
        playerReady = 0;
        port = p;
        tabSock = new Socket[max];
        is = new BufferedInputStream[max];
        os = new PrintWriter[max];
        try {

            serveurSock = new ServerSocket(port);
            (new ThreadServerConnection(this)).start();
            (new ThreadServerChat(this)).start();
        } catch (IOException e) {
            System.out.println(e);
        }
    }

    public void lireSocket(int i) {
        byte[] buf = new byte[200];
        String message;

        try {
            is[i].read(buf);
            message = ((new String(buf)).trim());
            
            dispatch(message, i);

        } catch (IOException e) {
            System.out.println(e);
        }
    }

    public void dispatch(String s, int j) {
        for (int i = 0; i < nbConnection; i++) {
            if (!(i == j)){
                os[i].print(s);
                os[i].flush();
            }

        }
    }

    public ServerSocket getServeurSock() {
        return serveurSock;
    }


    public void setServeurSock(ServerSocket serveurSock) {
        this.serveurSock = serveurSock;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public Socket getSock(int i) {
        return tabSock[i];
    }

    public BufferedInputStream getIs(int i) {
        return is[i];
    }
//////////////////////////ce thread est pour verifier a chaque fois, si il y a une demande de connection. si oui, l'ajouter

    class ThreadServerConnection extends Thread {

        ServerChat ref;

        public ThreadServerConnection(ServerChat r) {
            ref = r;
        }

        public void run() {
            while (ref.nbConnection < ref.tabSock.length) {

                try {
                    ref.tabSock[ref.nbConnection] = ref.serveurSock.accept();
                    is[ref.nbConnection] = new BufferedInputStream(tabSock[ref.nbConnection].getInputStream());
                    os[ref.nbConnection] = new PrintWriter(tabSock[ref.nbConnection].getOutputStream());
                    ref.nbConnection++;
                    ref.playerReady++;
                    System.out.println("Client connectés : "+ref.nbConnection);
                    if(ref.playerReady == 2)
                        
                    ref.dispatch("OpponentReadyStateCharged", 5);
                } catch (IOException e) {
                    System.out.println(e);
                }

            }
        }
    }
/////////////////////////ce thread est pour surveiller le flux de données entrant. et le dispatcher a tous les utilisateurs.

    class ThreadServerChat extends Thread {

        ServerChat ref;

        public ThreadServerChat(ServerChat r) {
            ref = r;
        }

        public void run() {
            while (true) {
                for (int i = 0; i < ref.nbConnection; i++) {
                    try {

                        if (ref.getIs(i).available() == 0) {
                            try {
                                Thread.sleep(100);
                            } catch (InterruptedException e) {
                                System.out.println(e);
                            }
                        } else {
                            ref.lireSocket(i);
                        }
                    } catch (IOException e) {
                        System.out.println(e);
                    }
                }
            }
        }
    }
}
